//
//  GroupViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/4.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "GroupViewController.h"
#import "GroupView.h"
//#import "GradeView.h"

@interface GroupViewController ()
{
    UIScrollView *_scrollView;
    NSDictionary *dataSource;
    GroupView *firstView;
    GroupView *secondView;
    GroupView *thirdView;
    GroupView *fourthView;
    GroupView *fifthView;
    GroupView *juneView;
    GroupView *julyView;
}
@end

@implementation GroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createGroupView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getUserInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] url:MMyTeam completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            dataSource = responseJson[@"data"];
            firstView.nameLabel.text = dataSource[@"oneUserName"];
            firstView.phoneLabel.text = dataSource[@"oneUserMobile"];
            firstView.referrerLabel.text = [NSString stringWithFormat:@"推荐人:%@",dataSource[@"oneUserReferee"]];
            firstView.headerImageView.image = [UIImage imageNamed:@"矢量智能对象1"];
            
            secondView.nameLabel.text = dataSource[@"twoUserName"];
            secondView.phoneLabel.text = dataSource[@"twoUserMobile"];
            secondView.referrerLabel.text = [NSString stringWithFormat:@"推荐人:%@",dataSource[@"twoUserReferee"]];
            secondView.headerImageView.image = [UIImage imageNamed:@"矢量智能对象6"];
            
            thirdView.nameLabel.text = dataSource[@"threeUserName"];
            thirdView.phoneLabel.text = dataSource[@"threeUserMobile"];
            thirdView.referrerLabel.text = [NSString stringWithFormat:@"推荐人:%@",dataSource[@"threeUserReferee"]];
            thirdView.headerImageView.image = [UIImage imageNamed:@"矢量智能对象5"];
            
            fourthView.nameLabel.text = dataSource[@"fourUserName"];
            fourthView.phoneLabel.text = dataSource[@"fourUserMobile"];
            fourthView.referrerLabel.text = [NSString stringWithFormat:@"推荐人:%@",dataSource[@"fourUserReferee"]];
            fourthView.headerImageView.image = [UIImage imageNamed:@"矢量智能对象4"];
            
            fifthView.nameLabel.text = dataSource[@"fiveUserName"];
            fifthView.phoneLabel.text = dataSource[@"fiveUserMobile"];
            fifthView.referrerLabel.text = [NSString stringWithFormat:@"推荐人:%@",dataSource[@"fiveUserReferee"]];
            fifthView.headerImageView.image = [UIImage imageNamed:@"矢量智能对象3"];
            
            juneView.nameLabel.text = dataSource[@"sixUserName"];
            juneView.phoneLabel.text = dataSource[@"sixUserMobile"];
            juneView.referrerLabel.text = [NSString stringWithFormat:@"推荐人:%@",dataSource[@"sixUserReferee"]];
            juneView.headerImageView.image = [UIImage imageNamed:@"矢量智能对象2"];
            
            julyView.nameLabel.text = dataSource[@"sevenUserName"];
            julyView.phoneLabel.text = dataSource[@"sevenUserMobile"];
            julyView.referrerLabel.text = [NSString stringWithFormat:@"推荐人:%@",dataSource[@"sevenUserReferee"]];
            julyView.headerImageView.image = [UIImage imageNamed:@"矢量智能对象7"];
            
            if ([dataSource[@"oneUserName"] isEqual:@""]) {
                firstView.nameLabel.text = @"暂无";
                firstView.phoneLabel.text = nil;
                firstView.referrerLabel.text = nil;
            }
            if ([dataSource[@"twoUserName"] isEqual:@""]) {
                secondView.nameLabel.text = @"暂无";
                secondView.phoneLabel.text = nil;
                secondView.referrerLabel.text = nil;
            }
            if ([dataSource[@"threeUserName"] isEqual:@""]) {
                thirdView.nameLabel.text = @"暂无";
                thirdView.phoneLabel.text = nil;
                thirdView.referrerLabel.text = nil;
            }
            if ([dataSource[@"fourUserName"] isEqual:@""]) {
                fourthView.nameLabel.text = @"暂无";
                fourthView.phoneLabel.text = nil;
                fourthView.referrerLabel.text = nil;
            }
            if ([dataSource[@"fiveUserName"] isEqual:@""]) {
                fifthView.nameLabel.text = @"暂无";
                fifthView.phoneLabel.text = nil;
                fifthView.referrerLabel.text = nil;
            }
            if ([dataSource[@"sixUserName"] isEqual:@""]) {
                juneView.nameLabel.text = @"暂无";
                juneView.phoneLabel.text = nil;
                juneView.referrerLabel.text = nil;
            }
            if ([dataSource[@"sevenUserName"] isEqual:@""]) {
                julyView.nameLabel.text = @"暂无";
                julyView.phoneLabel.text = nil;
                julyView.referrerLabel.text = nil;
            }
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createGroupView
{
    self.titleLabel.text = @"我的团队";
    self.backButton.hidden = NO;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight)];
    _scrollView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    firstView = [[GroupView alloc] init];
    firstView.nameLabel.textColor = [UIColor colorWithRed:0.25 green:0.56 blue:0.99 alpha:1];
    [_scrollView addSubview:firstView];
    firstView.sd_layout
    .leftSpaceToView(_scrollView, (kScreenWidth-kScreenWidth/4)/2)
    .topSpaceToView(_scrollView, kScreenWidth * 0.15)
    .widthIs(kScreenWidth/4)
    .heightIs(135);

    secondView = [[GroupView alloc] init];
    secondView.nameLabel.textColor = [UIColor colorWithRed:0.99 green:0.65 blue:0.22 alpha:1];
    [_scrollView addSubview:secondView];
    secondView.sd_layout
    .leftSpaceToView(_scrollView, kScreenWidth/8)
    .topSpaceToView(firstView, 5)
    .widthIs(kScreenWidth/4)
    .heightIs(135);

    thirdView = [[GroupView alloc] init];
    thirdView.nameLabel.textColor = [UIColor colorWithRed:0.99 green:0.65 blue:0.22 alpha:1];
    [_scrollView addSubview:thirdView];
    thirdView.sd_layout
    .rightSpaceToView(_scrollView, kScreenWidth/8)
    .topSpaceToView(firstView, 5)
    .widthIs(kScreenWidth/4)
    .heightIs(135);

    fourthView = [[GroupView alloc] init];
    [_scrollView addSubview:fourthView];
    fourthView.sd_layout
    .leftSpaceToView(_scrollView, 0)
    .topSpaceToView(secondView, 5)
    .widthIs(kScreenWidth/4)
    .heightIs(135);
    
    fifthView = [[GroupView alloc] init];
    [_scrollView addSubview:fifthView];
    fifthView.sd_layout
    .leftSpaceToView(fourthView, 0)
    .topSpaceToView(secondView, 5)
    .widthIs(kScreenWidth/4)
    .heightIs(135);
    
    juneView = [[GroupView alloc] init];
    [_scrollView addSubview:juneView];
    juneView.sd_layout
    .leftSpaceToView(fifthView, 0)
    .topSpaceToView(thirdView, 5)
    .widthIs(kScreenWidth/4)
    .heightIs(135);
    
    julyView = [[GroupView alloc] init];
    [_scrollView addSubview:julyView];
    julyView.sd_layout
    .leftSpaceToView(juneView, 0)
    .topSpaceToView(thirdView, 5)
    .widthIs(kScreenWidth/4)
    .heightIs(135);
        
//    GradeView *memberView = [[GradeView alloc] init];
//    memberView.imgView.backgroundColor = [UIColor colorWithRed:0.43 green:0.43 blue:0.43 alpha:1];
//    memberView.nameLabel.text = @"组员";
//    [_scrollView addSubview:memberView];
//    memberView.sd_layout
//    .rightSpaceToView(_scrollView, 0)
//    .topSpaceToView(julyView, 35)
//    .widthIs(50)
//    .heightIs(20);
//
//    GradeView *deputyView = [[GradeView alloc] init];
//    deputyView.imgView.backgroundColor = [UIColor colorWithRed:0.99 green:0.65 blue:0.22 alpha:1];
//    deputyView.nameLabel.text = @"副组长";
//    [_scrollView addSubview:deputyView];
//    deputyView.sd_layout
//    .rightSpaceToView(memberView, 5)
//    .topSpaceToView(julyView, 35)
//    .widthIs(50)
//    .heightIs(20);
//
//    GradeView *leaderView = [[GradeView alloc] init];
//    leaderView.nameLabel.text = @"组长";
//    [_scrollView addSubview:leaderView];
//    leaderView.sd_layout
//    .rightSpaceToView(deputyView, -5)
//    .topSpaceToView(julyView, 35)
//    .widthIs(50)
//    .heightIs(20);
    
    [_scrollView setupAutoContentSizeWithBottomView:julyView bottomMargin:90];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
